/* eslint-disable func-names */
/* eslint-disable no-param-reassign */
/* eslint-disable no-bitwise */
/* eslint-disable import/prefer-default-export */

const flowMap = [
    {
        type: 'StartEvent',
        icon: 'tebal_icon_user_default',
        bgColor: '#FFA22D'
    },
    {
        type: 'UserTask',
        icon: 'tebal_icon_user_default',
        bgColor: '#1890FF'
    },
    {
        type: 'ServiceTask',
        icon: 'tebal_icon_copier_default',
        bgColor: '#36CFC9'
    },
    {
        type: 'SequenceFlow',
        icon: 'gms_list_icon_zhidu_default',
        bgColor: '#ffa22d;'
    }
];
/**
 * 获取图标类型
 * @param {string} iconType
 */
const getIconType = iconType => flowMap.find(item => item.type === iconType) || {};

const detectZoom = () => {
    let ratio = 0;
    const { screen } = window;
    const ua = navigator.userAgent.toLowerCase();

    if (~ua.indexOf('firefox')) {
        if (window.devicePixelRatio !== undefined) {
            ratio = window.devicePixelRatio;
        }
    } else if (~ua.indexOf('msie')) {
        if (screen.deviceXDPI && screen.logicalXDPI) {
            ratio = screen.deviceXDPI / screen.logicalXDPI;
        }
    } else if (
        window.outerWidth !== undefined &&
        window.innerWidth !== undefined
    ) {
        ratio = window.outerWidth / window.innerWidth;
    }

    if (ratio) {
        ratio = Math.round(ratio * 100);
    }
    // 360安全浏览器下的innerWidth包含了侧边栏的宽度
    if (ratio !== 100) {
        if (ratio >= 95 && ratio <= 105) {
            ratio = 100;
        }
    }
    return ratio;
};
const watchScorll = ref => {
    const elems = ref.children;
    for (let i = 0; i < elems.length; i += 1) {
        (function(elem, lastClientX = null, lastClientY = null, pushed = null) {
            if (elem) {
                elem.addEventListener(
                    'mousedown',
                    e => {
                        pushed = 1;
                        lastClientX = e.clientX;
                        lastClientY = e.clientY;
                        e.preventDefault();
                        e.stopPropagation();
                    },
                    0
                );

                document.addEventListener(
                    'mousemove',
                    e => {
                        if (pushed) {
                            elem.parentNode.scrollLeft -=
                                -lastClientX + (lastClientX = e.clientX);
                            elem.parentNode.scrollTop -=
                                -lastClientY + (lastClientY = e.clientY);
                        }
                    },
                    0
                );

                document.addEventListener(
                    'mouseup',
                    () => {
                        pushed = 0;
                    },
                    0
                );
            }
        }(elems[i]));
    }
};
export { getIconType, detectZoom, watchScorll };
